/*
 * Copyright (c) 2009-2024 Arm Limited. All rights reserved.
 *
 * SPDX-License-Identifier: Apache-2.0
 *
 * Licensed under the Apache License, Version 2.0 (the License); you may
 * not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an AS IS BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

/*============================ INCLUDES ======================================*/
#if 1
#if defined(_RTE_)
#   include "RTE_Components.h"
#endif

#if defined(RTE_Acceleration_Arm_2D_Helper_PFB)

#include "arm_2d.h"

#ifdef RTE_Acceleration_Arm_2D_Scene2

#define __USER_SCENE2_IMPLEMENT__
#include "arm_2d_scene_2.h"
#include "arm_2d_scenes.h"
#include "arm_2d_helper.h"
#include "arm_2d_example_controls.h"

#include <stdlib.h>
#include <string.h>

#if defined(__clang__)
#   pragma clang diagnostic push
#   pragma clang diagnostic ignored "-Wunknown-warning-option"
#   pragma clang diagnostic ignored "-Wreserved-identifier"
#   pragma clang diagnostic ignored "-Wsign-conversion"
#   pragma clang diagnostic ignored "-Wpadded"
#   pragma clang diagnostic ignored "-Wcast-qual"
#   pragma clang diagnostic ignored "-Wcast-align"
#   pragma clang diagnostic ignored "-Wmissing-field-initializers"
#   pragma clang diagnostic ignored "-Wgnu-zero-variadic-macro-arguments"
#   pragma clang diagnostic ignored "-Wmissing-prototypes"
#   pragma clang diagnostic ignored "-Wunused-variable"
#   pragma clang diagnostic ignored "-Wgnu-statement-expression"
#   pragma clang diagnostic ignored "-Wdeclaration-after-statement"
#   pragma clang diagnostic ignored "-Wunused-function"
#   pragma clang diagnostic ignored "-Wmissing-declarations"  
#elif __IS_COMPILER_ARM_COMPILER_5__
#   pragma diag_suppress 64,177
#elif __IS_COMPILER_IAR__
#   pragma diag_suppress=Pa089,Pe188,Pe177,Pe174
#elif __IS_COMPILER_GCC__
#   pragma GCC diagnostic push
#   pragma GCC diagnostic ignored "-Wformat="
#   pragma GCC diagnostic ignored "-Wpedantic"
#   pragma GCC diagnostic ignored "-Wunused-function"
#   pragma GCC diagnostic ignored "-Wunused-variable"
#   pragma GCC diagnostic ignored "-Wincompatible-pointer-types"
#endif

/*============================ MACROS ========================================*/

#if __GLCD_CFG_COLOUR_DEPTH__ == 8

#   define c_tileCMSISLogo          c_tileCMSISLogoGRAY8

#elif __GLCD_CFG_COLOUR_DEPTH__ == 16

#   define c_tileCMSISLogo          c_tileCMSISLogoRGB565

#elif __GLCD_CFG_COLOUR_DEPTH__ == 32

#   define c_tileCMSISLogo          c_tileCMSISLogoCCCA8888
#else
#   error Unsupported colour depth!
#endif

/*============================ MACROFIED FUNCTIONS ===========================*/
#undef this
#define this (*ptThis)

/*============================ TYPES =========================================*/
/*============================ GLOBAL VARIABLES ==============================*/
char Tetris_go_back_flag = 0;
extern uint16_t key_num ;
void tank_90_init();
void tank_90_run_wait_time();
void tank_90_key_board_run();
void draw_my_90_tank(void *pTarget,                                  
                                const arm_2d_tile_t *ptTile,                    
                                bool bIsNewFrame) ;                   
                 

/*============================ PROTOTYPES ====================================*/
/*============================ LOCAL VARIABLES ===============================*/

/*! define dirty regions */
IMPL_ARM_2D_REGION_LIST(s_tDirtyRegions, static)

    /* a dirty region to be specified at runtime*/
    ADD_REGION_TO_LIST(s_tDirtyRegions,
        0  /* initialize at runtime later */
    ),
    
    /* add the last region:
        * it is the top left corner for text display 
        */
    ADD_LAST_REGION_TO_LIST(s_tDirtyRegions,
        .tLocation = {
            .iX = 0,
            .iY = 0,
        },
        .tSize = {
            .iWidth = 320,
            .iHeight = 240,
        },
    ),

END_IMPL_ARM_2D_REGION_LIST(s_tDirtyRegions)

/*============================ IMPLEMENTATION ================================*/

static void __on_scene2_load(arm_2d_scene_t *ptScene)
{
    user_scene_2_t *ptThis = (user_scene_2_t *)ptScene;
    ARM_2D_UNUSED(ptThis);

}

static void __on_scene2_depose(arm_2d_scene_t *ptScene)
{
    user_scene_2_t *ptThis = (user_scene_2_t *)ptScene;
    ARM_2D_UNUSED(ptThis);
    
    ptScene->ptPlayer = NULL;
    
    /* reset timestamp */
    arm_foreach(int64_t,this.lTimestamp, ptItem) {
        *ptItem = 0;
    }

    if (!this.bUserAllocated) {
        __arm_2d_free_scratch_memory(ARM_2D_MEM_TYPE_UNSPECIFIED, ptScene);
    }
}

/*----------------------------------------------------------------------------*
 * Scene 2                                                                    *
 *----------------------------------------------------------------------------*/

static void __on_scene2_background_start(arm_2d_scene_t *ptScene)
{
    user_scene_2_t *ptThis = (user_scene_2_t *)ptScene;
    ARM_2D_UNUSED(ptThis);

}

static void __on_scene2_background_complete(arm_2d_scene_t *ptScene)
{
    user_scene_2_t *ptThis = (user_scene_2_t *)ptScene;
    ARM_2D_UNUSED(ptThis);

}


static void __on_scene2_frame_start(arm_2d_scene_t *ptScene)
{
    user_scene_2_t *ptThis = (user_scene_2_t *)ptScene;
    ARM_2D_UNUSED(ptThis);
	tank_90_key_board_run();
	   /* switch to next scene after 3s */
    if (arm_2d_helper_is_time_out(300, &this.lTimestamp[0])) {
        tank_90_run_wait_time();
			  //Tetris_level_run();
    }
		//Tetris_run();
		

}

static void __on_scene2_frame_complete(arm_2d_scene_t *ptScene)
{
    user_scene_2_t *ptThis = (user_scene_2_t *)ptScene;
    ARM_2D_UNUSED(ptThis);
    if(Tetris_go_back_flag){
				Tetris_go_back_flag = 0;
				__arm_2d_scene1_init(   &DISP0_ADAPTER ,NULL);
				arm_2d_scene_player_switch_to_next_scene(ptScene->ptPlayer);
		}
   
}

static void __before_scene2_switching_out(arm_2d_scene_t *ptScene)
{
    user_scene_2_t *ptThis = (user_scene_2_t *)ptScene;
    ARM_2D_UNUSED(ptThis);

}
 uint8_t temp1 = 0;
uint8_t temp2 = 0;
uint8_t temp3 = 0;
uint8_t temp4 = 0;
static
IMPL_PFB_ON_DRAW(__pfb_draw_scene2_handler)
{
    user_scene_2_t *ptThis = (user_scene_2_t *)pTarget;
    arm_2d_size_t tScreenSize = ptTile->tRegion.tSize;

    ARM_2D_UNUSED(ptTile);
    ARM_2D_UNUSED(bIsNewFrame);
    ARM_2D_UNUSED(tScreenSize);
    
    arm_2d_canvas(ptTile, __top_canvas) {
    /*-----------------------draw the foreground begin-----------------------*/
        
        /* following code is just a demo, you can remove them */
        
        //arm_2d_fill_colour(ptTile, NULL, GLCD_COLOR_WHITE);

//        draw_my_Tetris(pTarget,                                  
//                 ptTile,                   
//                  bIsNewFrame);


        draw_my_90_tank(pTarget,                                  
                                ptTile,                    
                                 bIsNewFrame) ; 

        /* draw text at the top-left corner */

        arm_lcd_text_set_target_framebuffer((arm_2d_tile_t *)ptTile);
        arm_lcd_text_set_font(&ARM_2D_FONT_6x8.use_as__arm_2d_font_t);
        arm_lcd_text_set_draw_region(NULL);
        arm_lcd_text_set_colour(GLCD_COLOR_RED, GLCD_COLOR_WHITE);
        arm_lcd_text_location(0,0);
        //arm_lcd_printf("Scene 2=%d=%d=%d=%d",temp1,temp2,temp3,temp4);

    /*-----------------------draw the foreground end  -----------------------*/
    }
    arm_2d_op_wait_async(NULL);

    return arm_fsm_rt_cpl;
}

ARM_NONNULL(1)
user_scene_2_t *__arm_2d_scene2_init(   arm_2d_scene_player_t *ptDispAdapter, 
                                        user_scene_2_t *ptThis)
{
    bool bUserAllocated = false;
    assert(NULL != ptDispAdapter);

    s_tDirtyRegions[dimof(s_tDirtyRegions)-1].ptNext = NULL;

    /* get the screen region */
    arm_2d_region_t tScreen
        = arm_2d_helper_pfb_get_display_area(
            &ptDispAdapter->use_as__arm_2d_helper_pfb_t);

    /* initialise dirty region 0 at runtime
     * this demo shows that we create a region in the centre of a screen(320*240)
     * for a image stored in the tile c_tileCMSISLogoMask
     */
    //arm_2d_align_centre(tScreen, c_tileCMSISLogoMask.tRegion.tSize) {
        //s_tDirtyRegions[0].tRegion = __centre_region;
    //}

    //s_tDirtyRegions[dimof(s_tDirtyRegions)-1].tRegion.tSize.iWidth 
    //                                                    = tScreen.tSize.iWidth;
    
    if (NULL == ptThis) {
        ptThis = (user_scene_2_t *)
                    __arm_2d_allocate_scratch_memory(   sizeof(user_scene_2_t),
                                                        __alignof__(user_scene_2_t),
                                                        ARM_2D_MEM_TYPE_UNSPECIFIED);
        assert(NULL != ptThis);
        if (NULL == ptThis) {
            return NULL;
        }
    } else {
        bUserAllocated = true;
    }
    memset(ptThis, 0, sizeof(user_scene_2_t));

    *ptThis = (user_scene_2_t){
        .use_as__arm_2d_scene_t = {

            /* the canvas colour */
            .tCanvas = {GLCD_COLOR_WHITE}, 

            /* Please uncommon the callbacks if you need them
             */
            .fnOnLoad       = &__on_scene2_load,
            .fnScene        = &__pfb_draw_scene2_handler,
            .ptDirtyRegion  = (arm_2d_region_list_item_t *)s_tDirtyRegions,
            

            //.fnOnBGStart    = &__on_scene2_background_start,
            //.fnOnBGComplete = &__on_scene2_background_complete,
            .fnOnFrameStart = &__on_scene2_frame_start,
            //.fnBeforeSwitchOut = &__before_scene2_switching_out,
            .fnOnFrameCPL   = &__on_scene2_frame_complete,
            .fnDepose       = &__on_scene2_depose,

            .bUseDirtyRegionHelper = false,
        },
        .bUserAllocated = bUserAllocated,
    };

    /* ------------   initialize members of user_scene_2_t begin ---------------*/


    /* ------------   initialize members of user_scene_2_t end   ---------------*/

    arm_2d_scene_player_append_scenes(  ptDispAdapter, 
                                        &this.use_as__arm_2d_scene_t, 
                                        1);

		//Tetris_init();	
    tank_90_init();                                        
    return ptThis;
}
//===========================================================
//===========================================================================================

extern const arm_2d_tile_t c_tiledb01RGB565;//shi tou
extern const arm_2d_tile_t c_tiledb02RGB565;//zhuan kuai
extern const arm_2d_tile_t c_tiledb03RGB565;//chao di
extern const arm_2d_tile_t c_tiledb04RGB565;//shui
extern const arm_2d_tile_t c_tiledb05RGB565;//di zhuan

extern const arm_2d_tile_t c_tiletk01GRAY8;
extern const arm_2d_tile_t c_tiletk02GRAY8;
extern const arm_2d_tile_t c_tiletk03RGB565;

extern const arm_2d_tile_t c_tilejiaGRAY8;

extern const arm_2d_tile_t c_tiletank03_1RGB565;
extern const arm_2d_tile_t c_tiletank03_2RGB565;

extern const arm_2d_tile_t c_tiletank01_1GRAY8;
extern const arm_2d_tile_t c_tiletank01_2GRAY8;

extern const arm_2d_tile_t c_tiletank02_1GRAY8;
extern const arm_2d_tile_t c_tiletank02_2GRAY8;

const arm_2d_tile_t*  enemy_tk1_tiles[2] = {&c_tiletank01_1GRAY8,&c_tiletank02_1GRAY8};
const arm_2d_tile_t*  enemy_tk2_tiles[2] = {&c_tiletank01_2GRAY8,&c_tiletank02_2GRAY8};

const arm_2d_tile_t*  db_tiles[5] = {&c_tiledb01RGB565,&c_tiledb02RGB565,&c_tiledb03RGB565,&c_tiledb04RGB565,&c_tiledb05RGB565};

typedef struct{
    uint8_t iX;
    uint8_t iY;
    uint8_t iWidth;
    uint8_t iHeight;
    uint8_t Flag;
    //uint8_t level;
}tank_90_map_t;

#define Tank_90_MAP_NUMS (sizeof(my_tank_90_maps)/sizeof(my_tank_90_maps[0]))
#define TANK_90_MAP_WIDTH 10 

const tank_90_map_t my_tank_90_maps[] = {
    {.iX = 20/10,.iY = 20/10,.iWidth=6,.iHeight = 2,.Flag = 2,},
    {.iX = 60/10,.iY = 40/10,.iWidth=2,.iHeight = 8,.Flag = 2,},
    {.iX = 20/10,.iY = 100/10,.iWidth=4,.iHeight = 2,.Flag = 2,},
    {.iX = 20/10,.iY = 60/10,.iWidth=2,.iHeight = 4,.Flag = 2,},
    {.iX = 40/10,.iY = 60/10,.iWidth=2,.iHeight = 2,.Flag = 2,},
    
    {.iX = 80/10,.iY = 20/10,.iWidth=2,.iHeight = 10,.Flag = 3,},//r
    {.iX = 100/10,.iY = 40/10,.iWidth=2,.iHeight = 2,.Flag = 3,},
    {.iX = 120/10,.iY = 20/10,.iWidth=2,.iHeight = 2,.Flag = 3,},
    
    {.iX = 140/10,.iY = 40/10,.iWidth=2,.iHeight = 8,.Flag = 5,},//m
    {.iX = 160/10,.iY = 40/10,.iWidth=8,.iHeight = 2,.Flag = 5,},
    {.iX = 220/10,.iY = 60/10,.iWidth=2,.iHeight = 6,.Flag = 5,},
    {.iX = 180/10,.iY = 60/10,.iWidth=2,.iHeight = 4,.Flag = 5,},
    
    {.iX =  0/10,.iY = 120/10,.iWidth=6,.iHeight = 2,.Flag = 4,},//2
    {.iX = 40/10,.iY = 140/10,.iWidth=2,.iHeight = 4,.Flag = 4,},
    {.iX =  0/10,.iY = 160/10,.iWidth=4,.iHeight = 2,.Flag = 4,},
    {.iX =  0/10,.iY = 180/10,.iWidth=2,.iHeight = 4,.Flag = 4,},
    {.iX =  20/10,.iY = 200/10,.iWidth=4,.iHeight = 2,.Flag = 4,},
    
    {.iX =  180/10,.iY = 120/10,.iWidth=2,.iHeight = 10,.Flag = 1,},//d
    {.iX =  200/10,.iY = 200/10,.iWidth=2,.iHeight = 2,.Flag = 1,},
    {.iX =  220/10,.iY = 140/10,.iWidth=2,.iHeight = 6,.Flag = 1,},
    {.iX =  200/10,.iY = 120/10,.iWidth=2,.iHeight = 2,.Flag = 1,},
    
    {.iX =  100/10,.iY = 220/10,.iWidth=1,.iHeight = 2,.Flag = 2,}, //n   
    {.iX =  100/10,.iY = 210/10,.iWidth=4,.iHeight = 1,.Flag = 2,},        
    {.iX =  130/10,.iY = 220/10,.iWidth=1,.iHeight = 2,.Flag = 2,},
};

typedef struct{
    int8_t iX;
    int8_t iY;
    uint8_t Flag;
    uint8_t level;
    uint8_t direction;
    uint8_t time;
}Enemy_Tank_t;

#define TANK_90_ENEMY_NUM 10
Enemy_Tank_t tank_90_enemy[TANK_90_ENEMY_NUM]={
    {.iX = 12, .iY = 8, .Flag = 1, .level = 10,.direction = 1,.time = 0,},
    {.iX = 18, .iY = 0, .Flag = 2, .level = 10,.direction = 3,.time = 0,},
};

#define TANK_MAP_IY 24
#define TANK_MAP_IX 24
 
typedef struct{
    uint8_t map_level[TANK_MAP_IY][TANK_MAP_IX];
    uint8_t map_flag[TANK_MAP_IY][TANK_MAP_IX];
    int16_t tank_iX;
    int16_t tank_iY;
    uint8_t tank_direction;
    uint32_t rand_num;
    
    uint8_t enemy_direction[4];
    uint8_t enemy_direction_mod;
    
    
}tank_90_op_t;

tank_90_op_t tank_90_OP;

void tank_90_init(){
    uint16_t x,y,k;
    for(y = 0; y < TANK_MAP_IY; y++){
        for(x = 0; x < TANK_MAP_IX; x++){
            tank_90_OP.map_level[y][x] = 0;
            tank_90_OP.map_flag[y][x] = 0;
        }
    }
    for( k = 0; k < Tank_90_MAP_NUMS; k++ ){        
        for(y = my_tank_90_maps[k].iY; y < (my_tank_90_maps[k].iY + my_tank_90_maps[k].iHeight); y++ ){
            for(x = my_tank_90_maps[k].iX; x < (my_tank_90_maps[k].iX + my_tank_90_maps[k].iWidth); x++){
                tank_90_OP.map_level[y][x] = 100;
                tank_90_OP.map_flag[y][x] = my_tank_90_maps[k].Flag;
            }
        }
    }
    
    for(k = 2; k < TANK_90_ENEMY_NUM; k++ ){
        tank_90_enemy[k].Flag = 0;
        tank_90_enemy[k].level = 0;
        tank_90_enemy[k].time = 0;
    }
    
    tank_90_OP.tank_iX = 120;
    tank_90_OP.tank_iY = 180;
    tank_90_OP.tank_direction = 1;
    
    tank_90_OP.rand_num = 0;
}
#define MY_ABS(x) ((x) > 0 ? (x) : -(x))
uint8_t tank_90_check_move_stop(int16_t iX,int16_t iY){
    uint8_t x,y,i;
    if((iY < 0) || (iY > 220) || (iX < 0) || (iX > 220)){
        return 1;
    }
    x = iX/10;
    y = iY/10;
    if((2 == tank_90_OP.map_flag[y][x]) || (2 == tank_90_OP.map_flag[y][x+1]) || (2 == tank_90_OP.map_flag[y+1][x]) || (2 == tank_90_OP.map_flag[y+1][x+1])){
        return 1;
    }
    if((1 == tank_90_OP.map_flag[y][x]) || (1 == tank_90_OP.map_flag[y][x+1]) || (1 == tank_90_OP.map_flag[y+1][x]) || (1 == tank_90_OP.map_flag[y+1][x+1])){
        return 1;
    }
    if((5 == tank_90_OP.map_flag[y][x]) || (5 == tank_90_OP.map_flag[y][x+1]) || (5 == tank_90_OP.map_flag[y+1][x]) || (5 == tank_90_OP.map_flag[y+1][x+1])){
        return 1;
    }
    //tank_90_enemy[TANK_90_ENEMY_NUM]
    for(i = 0; i < TANK_90_ENEMY_NUM; i++){
        if(tank_90_enemy[i].Flag){
            if((MY_ABS(x - tank_90_enemy[i].iX ) < 2)  && (MY_ABS(y - tank_90_enemy[i].iY ) < 2)){
                return 1;
            }
        }
    }
    return 0;
}

uint8_t tank_90_check_enemy_move_stop(uint8_t num){
    uint8_t x,y,i;
    x = tank_90_enemy[num].iX;
    y = tank_90_enemy[num].iY;
    if((y < 0) || (y > 22) || (x < 0) || (x > 22)){
        return 1;
    }
    
    if((2 == tank_90_OP.map_flag[y][x]) || (2 == tank_90_OP.map_flag[y][x+1]) || (2 == tank_90_OP.map_flag[y+1][x]) || (2 == tank_90_OP.map_flag[y+1][x+1])){
        return 1;
    }
    if((1 == tank_90_OP.map_flag[y][x]) || (1 == tank_90_OP.map_flag[y][x+1]) || (1 == tank_90_OP.map_flag[y+1][x]) || (1 == tank_90_OP.map_flag[y+1][x+1])){
        return 1;
    }
    if((5 == tank_90_OP.map_flag[y][x]) || (5 == tank_90_OP.map_flag[y][x+1]) || (5 == tank_90_OP.map_flag[y+1][x]) || (5 == tank_90_OP.map_flag[y+1][x+1])){
        return 1;
    }
    if((4 == tank_90_OP.map_flag[y][x]) || (4 == tank_90_OP.map_flag[y][x+1]) || (4 == tank_90_OP.map_flag[y+1][x]) || (4 == tank_90_OP.map_flag[y+1][x+1])){
        return 1;
    }
    
    for(i = 0; i < TANK_90_ENEMY_NUM; i++){
        
        if(tank_90_enemy[i].Flag){
            if( i == num){
                continue;
            }
            if((MY_ABS(x - tank_90_enemy[i].iX ) < 2)  && (MY_ABS(y - tank_90_enemy[i].iY ) < 2)){
                return 1;
            }
        }
    }
    
    return 0;
}
void tank_90_enemy_move_new_direction(uint8_t num){
    //tank_90_enemy[num].direction++;
    
    tank_90_enemy[num].direction = (tank_90_OP.rand_num & 0x3) +1;
    tank_90_OP.rand_num++;
    
}
#define TANK_90_ENEMY_TIME 10
void tank_90_enemy_check_crossroads(uint8_t num){
    uint8_t x,y,i;
    x = tank_90_enemy[num].iX;
    y = tank_90_enemy[num].iY;
    if(tank_90_enemy[num].time < TANK_90_ENEMY_TIME){
        tank_90_enemy[num].time++;
    }
    if((y < 2) || (y > 20) || (x < 2) || (x > 20)){
        //return ;
    }
    if(tank_90_enemy[num].time >= TANK_90_ENEMY_TIME){
        if((3 == tank_90_enemy[num].direction) || (4 == tank_90_enemy[num].direction)){//zuo  you
            tank_90_OP.enemy_direction[0] = 3;
            tank_90_OP.enemy_direction[1] = 4;
            tank_90_OP.enemy_direction_mod = 2;
            i = 2;
            if(y >= 2){
                if((0 == tank_90_OP.map_flag[y-1][x]) || (0 == tank_90_OP.map_flag[y-1][x+1]) || (0 == tank_90_OP.map_flag[y-2][x]) || (0 == tank_90_OP.map_flag[y-2][x+1])){
                    tank_90_OP.enemy_direction[i] = 1;//shang
                    i++;
                    tank_90_OP.enemy_direction_mod++;                
                }
            }
            if(y <= 20){
                if((0 == tank_90_OP.map_flag[y+2][x]) || (0 == tank_90_OP.map_flag[y+3][x+1]) || (0 == tank_90_OP.map_flag[y+2][x]) || (0 == tank_90_OP.map_flag[y+3][x+1])){
                    tank_90_OP.enemy_direction[i] = 2;//xia
                    i++;
                    tank_90_OP.enemy_direction_mod++;                
                }
            }
            if(tank_90_OP.enemy_direction_mod > 2){
                tank_90_enemy[num].time = 0;
                tank_90_enemy[num].direction = tank_90_OP.enemy_direction[(tank_90_OP.rand_num % tank_90_OP.enemy_direction_mod)];           
            }
        }else{
            tank_90_OP.enemy_direction[0] = 1;
            tank_90_OP.enemy_direction[1] = 2;
            tank_90_OP.enemy_direction_mod = 2;
            i = 2;
            if(x >= 2){
                if((0 == tank_90_OP.map_flag[y][x-1]) || (0 == tank_90_OP.map_flag[y][x-2]) || (0 == tank_90_OP.map_flag[y+1][x-1]) || (0 == tank_90_OP.map_flag[y+1][x-2])){
                    tank_90_OP.enemy_direction[i] = 3;//zuo
                    i++;
                    tank_90_OP.enemy_direction_mod++;                
                }
            }
            if(x <= 20){
                if((0 == tank_90_OP.map_flag[y][x+2]) || (0 == tank_90_OP.map_flag[y][x+3]) || (0 == tank_90_OP.map_flag[y+1][x+2]) || (0 == tank_90_OP.map_flag[y+1][x+3])){
                    tank_90_OP.enemy_direction[i] = 4;//you
                    i++;
                    tank_90_OP.enemy_direction_mod++;                
                }
            }
            if(tank_90_OP.enemy_direction_mod > 2){
                tank_90_enemy[num].time = 0;
                tank_90_enemy[num].direction = tank_90_OP.enemy_direction[(tank_90_OP.rand_num % tank_90_OP.enemy_direction_mod)];           
            }
        }
    }
    
}
void tank_90_enemy_move(){
    uint8_t i;
    for(i = 0; i < TANK_90_ENEMY_NUM; i++){
        if(tank_90_enemy[i].Flag){
            switch(tank_90_enemy[i].direction){
                case 1://shang
                    tank_90_enemy[i].iY--;
                    if(tank_90_check_enemy_move_stop(i)){
                        tank_90_enemy[i].iY++;
                        tank_90_enemy_move_new_direction(i);
                    }else{
                        tank_90_enemy_check_crossroads(i);    
                    }
                break;
                case 2://xia
                    tank_90_enemy[i].iY++;
                    if(tank_90_check_enemy_move_stop(i)){
                        tank_90_enemy[i].iY--;
                        tank_90_enemy_move_new_direction(i);
                    }else{
                        tank_90_enemy_check_crossroads(i);    
                    }
                break;
                case 3://zuo
                    tank_90_enemy[i].iX--;
                    if(tank_90_check_enemy_move_stop(i)){
                        tank_90_enemy[i].iX++;
                        tank_90_enemy_move_new_direction(i);
                    }else{
                        tank_90_enemy_check_crossroads(i);    
                    }
                break;
                case 4://you
                    tank_90_enemy[i].iX++;
                    if(tank_90_check_enemy_move_stop(i)){
                        tank_90_enemy[i].iX--;
                        tank_90_enemy_move_new_direction(i);
                    }else{
                        tank_90_enemy_check_crossroads(i);    
                    }
                break;
            }
            
        }        
    }
}
void tank_90_move(){
    switch(tank_90_OP.tank_direction){
        case 1://shang
            tank_90_OP.tank_iY -= 10; 
            if(tank_90_check_move_stop(tank_90_OP.tank_iX,tank_90_OP.tank_iY)){
                tank_90_OP.tank_iY += 10;
            }
        break;
        case 2://xia
            tank_90_OP.tank_iY += 10; 
            if(tank_90_check_move_stop(tank_90_OP.tank_iX,tank_90_OP.tank_iY)){
                tank_90_OP.tank_iY -= 10;
            }
        break;
        case 3://zhuo
            tank_90_OP.tank_iX -= 10;
            if(tank_90_check_move_stop(tank_90_OP.tank_iX,tank_90_OP.tank_iY)){
                tank_90_OP.tank_iX += 10;
            }
        break;
        case 4://you
            tank_90_OP.tank_iX += 10;
            if(tank_90_check_move_stop(tank_90_OP.tank_iX,tank_90_OP.tank_iY)){
                tank_90_OP.tank_iX -= 10;
            }
        break;
    }
}

void tank_90_run_wait_time(){
    tank_90_enemy_move();
    tank_90_move();
    
    tank_90_OP.rand_num++;
}

void tank_90_key_board_run(){
    switch(key_num){
        case 2:
            tank_90_OP.tank_direction = 1;    
        break;
        case 8:
            tank_90_OP.tank_direction = 2;    
        break;
        case 4:
            tank_90_OP.tank_direction = 3;    
        break;
        case 6:
            tank_90_OP.tank_direction = 4;    
        break;
        case 14:
            
        break;
        case 15:
            //Soul_Douluo_Op.pause_flag = 1;    
        break;
        case 16:
            //Soul_Douluo_Op.emit_flag = 1;    
        break;
    }
    key_num = 0;
}

void draw_my_90_tank(void *pTarget,                                  
                                const arm_2d_tile_t *ptTile,                    
                                bool bIsNewFrame){
     arm_2d_fill_colour(ptTile, NULL, GLCD_COLOR_BLACK);
     do{
        uint16_t i;
        arm_2d_region_t myRegion ={
            .tLocation = {.iX = 240,.iY = 0},
            .tSize = {.iHeight = 240,.iWidth = 80},
        };
        arm_2d_fill_colour(ptTile, &myRegion, GLCD_COLOR_DARK_GREY);
     }while(0);
 
     do{
        uint16_t k,x,y;
        arm_2d_region_t myRegion ={
            .tLocation = {.iX = 0,.iY = 20},
            .tSize = {.iHeight = 10,.iWidth = 10},
        };
        for( k = 0; k < Tank_90_MAP_NUMS; k++ ){        
            for(y = my_tank_90_maps[k].iY; y < (my_tank_90_maps[k].iY + my_tank_90_maps[k].iHeight); y++ ){
                for(x = my_tank_90_maps[k].iX; x < (my_tank_90_maps[k].iX + my_tank_90_maps[k].iWidth); x++){
                    if(tank_90_OP.map_level[y][x]){
                        myRegion.tLocation.iX = x * TANK_90_MAP_WIDTH;
                        myRegion.tLocation.iY = y * TANK_90_MAP_WIDTH;
                        arm_2dp_rgb16_tile_copy_only(NULL,                            
                                db_tiles[tank_90_OP.map_flag[y][x] -1],
                                ptTile,        
                                &myRegion);
                    }                  
                }
            }
        }

        
    }while(0);   

    do{//tank
        arm_2d_region_t myRegion ={
            .tLocation = {.iX = 120,.iY = 180},
            .tSize = {.iHeight = 20,.iWidth = 20},
        };
        myRegion.tLocation.iX = tank_90_OP.tank_iX;
        myRegion.tLocation.iY = tank_90_OP.tank_iY;
 
        if(3 == tank_90_OP.tank_direction){//zuo
            arm_2dp_rgb16_tile_copy_with_colour_keying_only(NULL,
                                &c_tiletank03_1RGB565,                                
                                ptTile,        
                                &myRegion,
                                GLCD_COLOR_BLACK); 
        }else if(4 == tank_90_OP.tank_direction){//you
            
            arm_2dp_rgb16_tile_copy_with_colour_keying_and_x_mirror(NULL,
                                &c_tiletank03_1RGB565,                                
                                ptTile,        
                                &myRegion,
                                GLCD_COLOR_BLACK); 
        }else if(2 == tank_90_OP.tank_direction){//xia
            
            arm_2dp_rgb16_tile_copy_with_colour_keying_and_y_mirror(NULL,
                                &c_tiletank03_2RGB565,                                
                                ptTile,        
                                &myRegion,
                                GLCD_COLOR_BLACK); 
        }else{
            arm_2dp_rgb16_tile_copy_with_colour_keying_only(NULL,
                                &c_tiletank03_2RGB565,                                
                                ptTile,        
                                &myRegion,
                                GLCD_COLOR_BLACK); 
        }
             
    }while(0);
    //tank_90_enemy[TANK_90_ENEMY_NUM]
    do{
        arm_2d_region_t myRegion ={
            .tLocation = {.iX = 120,.iY = 80},
            .tSize = {.iHeight = 20,.iWidth = 20},
        };
        char i;
        for(i = 0; i < TANK_90_ENEMY_NUM; i++){
            if(tank_90_enemy[i].Flag ){
                myRegion.tLocation.iX = tank_90_enemy[i].iX * TANK_90_MAP_WIDTH;
                myRegion.tLocation.iY = tank_90_enemy[i].iY * TANK_90_MAP_WIDTH;
                if(3 == tank_90_enemy[i].direction){//zuo
                    arm_2d_fill_colour_with_mask_and_opacity(   
                            ptTile, 
                            &myRegion, 
                            enemy_tk1_tiles[tank_90_enemy[i].Flag - 1], 
                            (__arm_2d_color_t){GLCD_COLOR_WHITE},
                            200);    
                }else if(4 == tank_90_enemy[i].direction){//you                                         
                    arm_2d_fill_colour_with_mask_x_mirror_and_opacity (   
                            ptTile, 
                            &myRegion, 
                            enemy_tk1_tiles[tank_90_enemy[i].Flag - 1], 
                            (__arm_2d_color_t){GLCD_COLOR_WHITE},
                            200);              
                }else if(2 == tank_90_enemy[i].direction){//xia                                         
                    arm_2d_fill_colour_with_mask_y_mirror_and_opacity (   
                            ptTile, 
                            &myRegion, 
                            enemy_tk2_tiles[tank_90_enemy[i].Flag - 1], 
                            (__arm_2d_color_t){GLCD_COLOR_WHITE},
                            200);              
                }else{
                    arm_2d_fill_colour_with_mask_and_opacity (   
                            ptTile, 
                            &myRegion, 
                            enemy_tk2_tiles[tank_90_enemy[i].Flag - 1], 
                            (__arm_2d_color_t){GLCD_COLOR_WHITE},
                            200);
                }
                
            }
        }
        
                                                     
    }while(0);

    //c_tilejiaGRAY8   
     do{
        arm_2d_region_t myRegion ={
            .tLocation = {.iX = 110,.iY = 220},
            .tSize = {.iHeight = 20,.iWidth = 20},
        };

        arm_2d_fill_colour_with_mask_and_opacity(   
                            ptTile, 
                            &myRegion, 
                            &c_tilejiaGRAY8, 
                            (__arm_2d_color_t){GLCD_COLOR_WHITE},
                            250);
                                                     
    }while(0);    
                                    
}
                                

                                
void draw_my_90_tank2(void *pTarget,                                  
                                const arm_2d_tile_t *ptTile,                    
                                bool bIsNewFrame){
     arm_2d_fill_colour(ptTile, NULL, GLCD_COLOR_BLACK);   

     do{
        arm_2d_region_t myRegion ={
            .tLocation = {.iX = 0,.iY = 20},
            .tSize = {.iHeight = 20,.iWidth = 100},
        };
        arm_2dp_rgb16_tile_fill_only(NULL,
                            &c_tiledb01RGB565,       
                            ptTile,        
                            &myRegion); 
    }while(0);  
    do{
        arm_2d_region_t myRegion ={
            .tLocation = {.iX = 0,.iY = 60},
            .tSize = {.iHeight = 20,.iWidth = 100},
        };
        arm_2dp_rgb16_tile_fill_only(NULL,
                            &c_tiledb02RGB565,       
                            ptTile,        
                            &myRegion); 
    }while(0); 
    do{
        arm_2d_region_t myRegion ={
            .tLocation = {.iX = 0,.iY = 100},
            .tSize = {.iHeight = 20,.iWidth = 100},
        };
        arm_2dp_rgb16_tile_fill_only(NULL,
                            &c_tiledb03RGB565,       
                            ptTile,        
                            &myRegion); 
    }while(0); 
    do{
        arm_2d_region_t myRegion ={
            .tLocation = {.iX = 0,.iY = 140},
            .tSize = {.iHeight = 20,.iWidth = 100},
        };
        arm_2dp_rgb16_tile_fill_only(NULL,
                            &c_tiledb04RGB565,       
                            ptTile,        
                            &myRegion); 
    }while(0);
    do{
        arm_2d_region_t myRegion ={
            .tLocation = {.iX = 0,.iY = 180},
            .tSize = {.iHeight = 20,.iWidth = 100},
        };
        arm_2dp_rgb16_tile_fill_only(NULL,
                            &c_tiledb05RGB565,       
                            ptTile,        
                            &myRegion); 
    }while(0);
    
    do{
        arm_2d_region_t myRegion ={
            .tLocation = {.iX = 120,.iY = 180},
            .tSize = {.iHeight = 20,.iWidth = 20},
        };
        arm_2dp_rgb16_tile_fill_only(NULL,
                            &c_tiletk03RGB565,       
                            ptTile,        
                            &myRegion); 
    }while(0);
    do{
        arm_2d_region_t myRegion ={
            .tLocation = {.iX = 120,.iY = 80},
            .tSize = {.iHeight = 20,.iWidth = 20},
        };

        arm_2d_fill_colour_with_mask_and_opacity(   
                            ptTile, 
                            &myRegion, 
                            &c_tiletk01GRAY8, 
                            (__arm_2d_color_t){GLCD_COLOR_WHITE},
                            200);
                                                     
    }while(0);
    do{
        arm_2d_region_t myRegion ={
            .tLocation = {.iX = 180,.iY = 60},
            .tSize = {.iHeight = 20,.iWidth = 20},
        };

        arm_2d_fill_colour_with_mask_and_opacity(   
                            ptTile, 
                            &myRegion, 
                            &c_tiletk02GRAY8, 
                            (__arm_2d_color_t){GLCD_COLOR_WHITE},
                            200);
                                                     
    }while(0);
                                    
}




const tank_90_map_t my_tank_90_maps2[] = {
    {.iX = 20/10,.iY = 20/10,.iWidth=2,.iHeight = 2,.Flag = 2,},
    {.iX = 40/10,.iY = 20/10,.iWidth=2,.iHeight = 2,.Flag = 2,},
    {.iX = 60/10,.iY = 20/10,.iWidth=2,.iHeight = 2,.Flag = 2,},
    //{.iX = 80/10,.iY = 20/10,.iWidth=2,.iHeight = 2,.Flag = 2,},
    
    {.iX = 60/10,.iY = 40/10,.iWidth=2,.iHeight = 2,.Flag = 2,},
    {.iX = 60/10,.iY = 60/10,.iWidth=2,.iHeight = 2,.Flag = 2,},
    {.iX = 60/10,.iY = 80/10,.iWidth=2,.iHeight = 2,.Flag = 2,},
    {.iX = 60/10,.iY = 100/10,.iWidth=2,.iHeight = 2,.Flag = 2,},
    
    //{.iX = 60/10,.iY = 100/10,.iWidth=2,.iHeight = 2,.Flag = 2,.level = 100},
    {.iX = 40/10,.iY = 100/10,.iWidth=2,.iHeight = 2,.Flag = 2,},
    {.iX = 20/10,.iY = 100/10,.iWidth=2,.iHeight = 2,.Flag = 2,},
    
    {.iX = 20/10,.iY = 80/10,.iWidth=2,.iHeight = 2,.Flag = 2,},
    {.iX = 20/10,.iY = 60/10,.iWidth=2,.iHeight = 2,.Flag = 2,},
    
    {.iX = 40/10,.iY = 60/10,.iWidth=2,.iHeight = 2,.Flag = 2,},
    //{.iX = 20/10,.iY = 40/10,.iWidth=2,.iHeight = 2,.Flag = 2,.level = 100},
    
    {.iX = 80/10,.iY = 20/10,.iWidth=2,.iHeight = 2,.Flag = 3,},//r
    {.iX = 80/10,.iY = 40/10,.iWidth=2,.iHeight = 2,.Flag = 3,},
    {.iX = 80/10,.iY = 60/10,.iWidth=2,.iHeight = 2,.Flag = 3,},
    {.iX = 80/10,.iY = 80/10,.iWidth=2,.iHeight = 2,.Flag = 3,},
    {.iX = 80/10,.iY = 100/10,.iWidth=2,.iHeight = 2,.Flag = 3,},
    
    {.iX = 100/10,.iY = 40/10,.iWidth=2,.iHeight = 2,.Flag = 3,},
    {.iX = 120/10,.iY = 20/10,.iWidth=2,.iHeight = 2,.Flag = 3,},
    
    //{.iX = 140/10,.iY = 20/10,.iWidth=2,.iHeight = 2,.Flag = 5,.level = 100},//m
    {.iX = 140/10,.iY = 40/10,.iWidth=2,.iHeight = 2,.Flag = 5,},
    {.iX = 140/10,.iY = 60/10,.iWidth=2,.iHeight = 2,.Flag = 5,},
    {.iX = 140/10,.iY = 80/10,.iWidth=2,.iHeight = 2,.Flag = 5,},
    {.iX = 140/10,.iY = 100/10,.iWidth=2,.iHeight = 2,.Flag = 5,},
    
    {.iX = 160/10,.iY = 40/10,.iWidth=2,.iHeight = 2,.Flag = 5,},
    {.iX = 180/10,.iY = 40/10,.iWidth=2,.iHeight = 2,.Flag = 5,},
    {.iX = 200/10,.iY = 40/10,.iWidth=2,.iHeight = 2,.Flag = 5,},
    {.iX = 220/10,.iY = 40/10,.iWidth=2,.iHeight = 2,.Flag = 5,},
    
    {.iX = 220/10,.iY = 60/10,.iWidth=2,.iHeight = 2,.Flag = 5,},
    {.iX = 220/10,.iY = 80/10,.iWidth=2,.iHeight = 2,.Flag = 5,},
    {.iX = 220/10,.iY = 100/10,.iWidth=2,.iHeight = 2,.Flag = 5,},
    
    {.iX = 180/10,.iY = 60/10,.iWidth=2,.iHeight = 2,.Flag = 5,},
    {.iX = 180/10,.iY = 80/10,.iWidth=2,.iHeight = 2,.Flag = 5,},
    
    {.iX =  0/10,.iY = 120/10,.iWidth=2,.iHeight = 2,.Flag = 4,},//2
    {.iX = 20/10,.iY = 120/10,.iWidth=2,.iHeight = 2,.Flag = 4,},
    {.iX = 40/10,.iY = 120/10,.iWidth=2,.iHeight = 2,.Flag = 4,},
    
    {.iX = 40/10,.iY = 140/10,.iWidth=2,.iHeight = 2,.Flag = 4,},
    {.iX = 40/10,.iY = 160/10,.iWidth=2,.iHeight = 2,.Flag = 4,},
    
    {.iX = 20/10,.iY = 160/10,.iWidth=2,.iHeight = 2,.Flag = 4,},
    {.iX =  0/10,.iY = 160/10,.iWidth=2,.iHeight = 2,.Flag = 4,},
    
    {.iX =  0/10,.iY = 180/10,.iWidth=2,.iHeight = 2,.Flag = 4,},
    {.iX =  0/10,.iY = 200/10,.iWidth=2,.iHeight = 2,.Flag = 4,},
    
    {.iX =  20/10,.iY = 200/10,.iWidth=2,.iHeight = 2,.Flag = 4,},
    {.iX =  40/10,.iY = 200/10,.iWidth=2,.iHeight = 2,.Flag = 4,},
    
    {.iX =  180/10,.iY = 120/10,.iWidth=2,.iHeight = 2,.Flag = 1,},
    {.iX =  180/10,.iY = 140/10,.iWidth=2,.iHeight = 2,.Flag = 1,},//d
    {.iX =  180/10,.iY = 160/10,.iWidth=2,.iHeight = 2,.Flag = 1,},
    {.iX =  180/10,.iY = 180/10,.iWidth=2,.iHeight = 2,.Flag = 1,},
    {.iX =  180/10,.iY = 200/10,.iWidth=2,.iHeight = 2,.Flag = 1,},
    
    {.iX =  200/10,.iY = 200/10,.iWidth=2,.iHeight = 2,.Flag = 1,},
    //{.iX =  220/10,.iY = 200/10,.iWidth=2,.iHeight = 2,.Flag = 1,.level = 100},
    
    {.iX =  220/10,.iY = 180/10,.iWidth=2,.iHeight = 2,.Flag = 1,},
    {.iX =  220/10,.iY = 160/10,.iWidth=2,.iHeight = 2,.Flag = 1,},
    {.iX =  220/10,.iY = 140/10,.iWidth=2,.iHeight = 2,.Flag = 1,},
    
    {.iX =  200/10,.iY = 120/10,.iWidth=2,.iHeight = 2,.Flag = 1,},
    
    
    //{.iX =  220/10,.iY = 200/10,.iWidth=2,.iHeight = 2,.Flag = 2,.level = 100},
    
    {.iX =  100/10,.iY = 220/10,.iWidth=1,.iHeight = 2,.Flag = 2,},
    
    {.iX =  100/10,.iY = 210/10,.iWidth=2,.iHeight = 1,.Flag = 2,},
    {.iX =  120/10,.iY = 210/10,.iWidth=2,.iHeight = 1,.Flag = 2,},
    
    {.iX =  130/10,.iY = 220/10,.iWidth=1,.iHeight = 2,.Flag = 2,},
    
};




#if defined(__clang__)
#   pragma clang diagnostic pop
#endif

#endif

#endif
#endif
